package be.bonamis.timesheet.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import be.bonamis.timesheet.dto.ProjectDTO;

public class Import {
	public static List<ProjectDTO> importFromFile(String fileName){
		InputStream in = Import.class.getClassLoader().getResourceAsStream(fileName); 
	    HSSFWorkbook workbook;
	    List<ProjectDTO> list = new ArrayList<ProjectDTO>();
		try {
			workbook = new HSSFWorkbook(in);
		    HSSFSheet sheet = workbook.getSheetAt(0);		     

		    Iterator<Row> rowIterator = sheet.iterator();
		    while(rowIterator.hasNext()) {
		        Row row = rowIterator.next();		        
		        Cell cell01 = row.getCell(1);		        
		        ProjectDTO projectDTO = new ProjectDTO();		
				projectDTO.setName(cell01.getStringCellValue());
		        list.add(projectDTO);
		    }	
		} catch (IOException e) {
			e.printStackTrace();
		}     
		return list;
	}
}
